package com.rmfyzxfw.caseprocess.config.datasource;

import com.rmfyzxfw.caseprocess.enums.DynamicDatabaseTypeEnum;
import com.rmfyzxfw.caseprocess.model.po.DynamicDataSource;

public class DataSourceContextHolder {
    private static final ThreadLocal<String> dataSourceKeyContextHolder = new ThreadLocal<>();
    private static final ThreadLocal<DynamicDataSource> dataSourceContextHolder = new ThreadLocal<>();

    public static void setDataSourceKey(String dataSourceKey) {
        dataSourceKeyContextHolder.set(dataSourceKey);
    }

    public static String getDataSourceKey() {
        return dataSourceKeyContextHolder.get();
    }

    public static void clearDataSourceKey() {
        dataSourceKeyContextHolder.remove();
    }


    public static void setDataSource(DynamicDataSource dataSource) {
        dataSourceContextHolder.set(dataSource);
    }
    public static DynamicDataSource getDataSource() {
        return dataSourceContextHolder.get();
    }

    public static DynamicDatabaseTypeEnum getDataSourceType() {
        DynamicDataSource dataSource = getDataSource();
        if (dataSource == null) {
            return null;
        }
        return DynamicDatabaseTypeEnum.ofCode(dataSource.getDatabaseType());
    }

    public static void clearDataSource() {
        dataSourceContextHolder.remove();
    }

//    public static boolean needConvert() {
//        DynamicDataSource dataSource = getDataSource();
//        if (dataSource == null) {
//            return false;
//        }
//        return dataSource.needConvert();
//    }
}
